<?php
/**
 * ============================================================================
 * Created by lemocms.
 * 版权所有 2018-2027 lemocms，并保留所有权利。
 * 网站地址: https://www.lemocms.com
 * ----------------------------------------------------------------------------
 * 采用最新Thinkphp6实现
 * ============================================================================
 * User: yuege
 * Date: 2020/2/10
 * Time: 18:51
 */

namespace app\admin\controller\sys;

use app\common\controller\Backend;
use app\common\traits\Curd;
use lemo\helper\DataHelper;
use app\common\model\Attach as AttachModel;
use think\facade\Db;
use think\facade\Request;

class  Attach extends Backend
{
    use Curd;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function index()
    {
        $mime = input('mime');
        if (Request::isPost()) {
            $keys = $this->request->post('keys', '', 'trim');
            $page = $this->request->post('page') ? $this->request->post('page') : 1;
            $list = Db::name('attach')
                ->where('name', 'like', "%" . $keys . "%")
                ->where('mime', 'like', "%" . $mime . "%")
                ->order('id desc')
                ->paginate(['list_rows' => $this->pageSize, 'page' => $page])
                ->toArray();
            return $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['data'], 'count' => $list['total']];
        }
        $view = ['mime' => $mime];
        return view('', $view);
    }

    /*** 选择附件*/
    public function select()
    {
        $mime = input('mime');
        if (Request::isPost()) {
            $keys = $this->request->post('keys', '', 'trim');
            $page = $this->request->post('page') ? $this->request->post('page') : 1;
            $list = Db::name('attach')
                ->where('name', 'like', "%" . $keys . "%")
                ->where('mime', 'like', "%" . $mime . "%")
                ->order('id desc')
                ->paginate(['list_rows' => $this->pageSize, 'page' => $page])
                ->toArray();
            return $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['data'], 'count' => $list['total']];
        }
        $view = ['mime' => $mime];
        return view('', $view);
    }

    /*** 添加*/
    public function add()
    {

        return view('', ['title' => '上传', 'info' => []]);
    }


    /*** 编辑*/
    public function state()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $id = $this->request->post('id');
            if (empty($id)) {
                $this->error('id' . lang('not exist'));
            }

            $model = new AttachModel();
            $model->state($data);
            $this->success(lang('edit success'));
        }
    }

    public function delete($ids = "")
    {
        if ($ids) {
            $model = new AttachModel();
            $attacht = $model::where('id', 'in', $ids)->select();
            foreach ($attacht as $v) {
                @unlink(app()->getRootPath() . 'public' . $v->path);
                $v->delete();
            }

            $this->success('delete success');
        }
        $this->error(lang('ids is not right'));
    }

}

